﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using System.Collections.Generic;
using Brettle.Web.NeatUpload;
using Common.Logging;

public partial class Notice : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetCurrentClassLogger();
    private DaNotice dalNotice = DevFuture.Common.WorkContext.AppCtx.GetObject("DaNotice") as DaNotice;
    private DaTfiles dalTfile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;
    private DaTuser dauser = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        AspNetPager.PageSize = 10;

        if (!IsPostBack)
        {
            //FCKeditorContent.FormatOutput = true;
            //FCKeditorContent.FormatSource = true;

        
            if (Request.QueryString["type"] == "1")//定向
            {
                ViewState["TYPE"] = 1;
                span_create_all.Visible = false;
            }
            else//公告
            {
                ViewState["TYPE"] = 0;
                span_create_one.Visible = false;
            }

            this.QueryNotice();

            if (Request.QueryString["noticeid"] != null && !string.IsNullOrEmpty(Request.QueryString["noticeid"].ToString()))
            {
                this.DisplayDetailView(Request.QueryString["noticeid"].ToString());
            }

            SetUIByRights(Request.QueryString["type"]);
            

        }
    }

    private void QueryNotice()
    {
        int pagecount = 0;
        string strCondition = "where 1=1 ";

        if (context.RoleID != "Administrators")
        {
            strCondition = strCondition + " and uid='" + context.UserID + "'";
        }
        if(!string.IsNullOrEmpty(ViewState["TYPE"].ToString()))
        {
            strCondition = strCondition + " and Publish_Type='" + ViewState["TYPE"].ToString() + "'";
        }
        this.Repeater.DataSource = dalNotice.GetNoticeRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        Repeater.DataBind();

        AspNetPager.RecordCount = pagecount;
    } 

    protected bool DeleteInfo(string strKey)
    {
        //执行删除操作。
        return dalNotice.DeleteNotice(strKey);
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.QueryNotice();
    }

    protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            string strKey = e.CommandArgument.ToString();
            try
            {
                this.DeleteInfo(strKey);
                Utility.DeleteNoticeContent(strKey);

                this.QueryNotice();
                OperationResult.Text = "删除消息成功!"; divResult.Visible = true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                OperationResult.Text = "删除消息失败!"; divResult.Visible = true;
            }
        }
        else
        {
            //detail
            string strKey = e.CommandArgument.ToString();
            ViewState["NOWID"] = strKey;
            DisplayDetailView(strKey);

        }
        //else
        //{ 
        //    //edit
        //    string strKey = e.CommandArgument.ToString();
        //    DisplayEditView(strKey);
        //}
    }

    private void DisplayDetailView(string noticeid)
    {
        DataSet mod = dalNotice.GetNoticeRecords(" where notice_id='" + noticeid + "'");
        labCreate_Time.Text = mod.Tables[0].Rows[0]["Create_Time"].ToString();
        labDisplay_Name.Text = mod.Tables[0].Rows[0]["Display_Name"].ToString();
        labNotice_ID.Text = noticeid;
        labPublish_Type_Name.Text = mod.Tables[0].Rows[0]["Publish_Type_Name"].ToString();
        labTitle.Text = mod.Tables[0].Rows[0]["Title"].ToString();
        noticeframe.Attributes.Remove("src");
        noticeframe.Attributes.Add("src", "NoticeFile\\" + noticeid + ".htm");
        labAttachFile.Text = "<a href='#' onclick='javascript:InvokePopUploadParFile(\""+noticeid+"\");'>添加附件</a>";

        RepFile.DataSource = dalTfile.QueryFileRelation(noticeid);
        RepFile.DataBind();

        this.SetPanelVisible(DetailPanel);
    } 

    protected void RepFile_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strKey = e.CommandName.ToString();

        try
        {
            dalTfile.DeleteOneAttachFile(strKey);
            //this.QueryNotice();
            this.DisplayDetailView(labNotice_ID.Text);
            OperationResult.Text = "删除附件成功!"; divResult.Visible = true;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除附件失败!"; divResult.Visible = true;
        }
    }

    protected void btnDetailReturn_Click(object sender, EventArgs e)
    {
        this.SetPanelVisible(SummaryPanel);
    }

    protected void btnAttachFile_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(labNotice_ID.Text))
        {
            Response.Redirect("uploadfiles.aspx?notice&id=" + labNotice_ID.Text + "&m=0");
        }
    } 

    public string Substring(string strContent)
    {
        return strContent.Length > 30 ? strContent.Substring(0, 30) + "..." : strContent;
    }

    protected void SetPanelVisible(Panel panel)
    {
        SummaryPanel.Visible = false;
        DetailPanel.Visible = false;

        panel.Visible = true;
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        this.DisplayDetailView(ViewState["NOWID"].ToString());
    }


    protected void SetUIByRights(string type)
    {
        //在这里添加内部权限检查，并放到对象初始化代码之后
        if (type == "1")
        {
            span_create_one.Visible = dauser.CheckUserPrivilege(context.UserID, "Notice.createone");  
        }
        else
        { 
            span_create_all.Visible = dauser.CheckUserPrivilege(context.UserID, "Notice.createall");
        }
    }
}
